<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2018/2/1
 * Time: 22:08
 */

namespace controller;


use controller\base\Base;
use model\Releases;
use model\SysVisit;
use model\Visit;

class Debug extends Base
{
    public function index($id, $keys = '', $page = 1, $li = 1)
    {
        $this->header();
        // 1 时间 3票数
        if ($li != 3) {
            $li = 1;
        }
        $limit    = 20;
        $offset   = ($page - 1) * $limit;
        $releases = new Releases();
        if (!empty($keys)) {
            $sql      = "select * from (select releases.*,@rank := @rank + 1 as rank from releases, ( SELECT @rank := 0 ) r where action_id=:action_id and is_pass='true' and (number = :number or shop_name like :shop_name)";
            $sqlCount = "select count(1) num from releases where action_id=:action_id and is_pass='true'  and (number = :number or shop_name like :shop_name)";

            $params               = [];
            $params[':action_id'] = $id;
            $params[':number']    = $keys;
            $params[':shop_name'] = '%' . $keys . '%';

            if ($li == 1) {
                $sql .= " order by created desc)releases  limit $limit offset $offset";
            } else {
                $sql .= " order by poll desc)releases limit $limit offset $offset";
            }
            $count = $releases->getDb()->select($sqlCount, $params);
            $count = $count[0]['num'];
            $pages = ceil($count / $limit);
            $data  = $releases->getDb()->select($sql, $params);
        } else {
            $count = $releases->count('id', ['action_id' => $id, 'is_pass' => 'true']);
            $pages = ceil($count / $limit);
            $sql   = "select * from (select releases.*,@rank := @rank + 1 as rank from releases, ( SELECT @rank := 0 ) r where action_id=:action_id and is_pass='true'";

            $params               = [];
            $params[':action_id'] = $id;
            if ($li == 1) {
                $sql .= " order by created desc)releases limit $limit offset $offset";
            } else {
                $sql .= " order by poll desc)releases limit $limit offset $offset";
            }
            $data = $releases->getDb()->select($sql, $params);
        }

        // 统计
        $visit = new Visit();
        $visit->insert([
            'ip'        => ip(),
            'created'   => now(),
            'openid'    => session('openid'),
            'action_id' => $id
        ]);
        $visitCount = $visit->count('id', ['action_id' => $id]);
        // 系统加流量
        $sysVisit      = new SysVisit();
        $sysVisitCount = $sysVisit->sum('look_num', ['action_id' => $id]);
        $visitCount    = $visitCount + $sysVisitCount;

        // 投票人次
        $voteCount = $releases->sum('poll', ['action_id' => $id]);

        $this->assign('keys', $keys);
        $this->assign('li', $li);
        $this->assign('page', $page);
        $this->assign('data', $data);
        $this->assign('pages', $pages);
        $this->assign('count', $count);
        $this->assign('voteCount', $voteCount);
        $this->assign('visitCount', $visitCount);

        $this->fetch();
    }

    /**
     * @param int $id id
     * @param int $rid rid
     */
    public function view($id, $rid)
    {
        $this->header();
        $releases = new Releases();

        $sql                  = "select * from (select releases.*,@rank := @rank + 1 as rank from releases, ( SELECT @rank := 0 ) r where action_id=:action_id order by poll desc)releases where id=:id ";
        $params               = [];
        $params[':action_id'] = $id;
        $params[':id']        = $rid;
        $data                 = $releases->getDb()->row($sql, $params);

        if ($data['rank'] != 1) {
            $sql                  = "select * from (select releases.*,@rank := @rank + 1 as rank from releases, ( SELECT @rank := 0 ) r where action_id=:action_id order by poll desc)releases where rank =:rank ";
            $params               = [];
            $params[':action_id'] = $id;
            $params[':rank']      = $data['rank'] - 1;
            $preData              = $releases->getDb()->row($sql, $params);
            $prePoll              = $preData['poll'] - $data['poll'];
        } else {
            $prePoll = 0;
        }

        if (!empty($data)) {
            $img    = $data['images'];
            $images = explode(',', $img);
            $this->assign('images', $images);
            $this->assign('data', $data);
            $this->assign('prePoll', $prePoll);
            $this->fetch();
        }
    }
}